// pages/privacy/privacy.js
Page({
  data: {
    privacySettings: {
      recommendToWechatFriends: false,
      enableLocation: false,
      recommendToNearbyUsers: false,
      searchByWechatId: true
    }
  },

  onLoad() {
    // 从缓存加载隐私设置
    const savedSettings = wx.getStorageSync('privacySettings');
    if (savedSettings) {
      this.setData({
        privacySettings: savedSettings
      });
    }
  },

  // 切换开关状态
  toggleSetting(e) {
    const key = e.currentTarget.dataset.key;
    this.setData({
      [`privacySettings.${key}`]: e.detail.value
    });
  },

  // 保存设置
  saveSettings() {
    wx.setStorageSync('privacySettings', this.data.privacySettings);
    
    // 实际开发中这里应该调用API同步到服务器
    // this.syncToServer();
    
    wx.showToast({
      title: '保存成功',
      icon: 'success'
    });
    
    setTimeout(() => {
      wx.navigateBack();
    }, 1500);
  },

  // 同步到服务器（示例）
  syncToServer() {
    wx.request({
      url: 'https://your-api.com/updatePrivacy',
      method: 'POST',
      data: this.data.privacySettings,
      success: (res) => {
        console.log('隐私设置已同步到服务器');
      }
    });
  }
});